class AdminController < ApplicationController
  before_filter :require_admin
  skip_before_filter :verify_authenticity_token, :only => [:auto_complete_for_user_email]
  
  layout 'admin', :except => ['list']
  
  auto_complete_for :user, :email
  
  def index
    @logged_in_count = User.logged_in.count
    @count = Print.find_all_to_print.length 
    @count_unread = Message.find(:all, :conditions => ['read = \'f\' and recipient = ?', current_user.id ]).length
  end

  def payments
    @payments = Payment.paginate :page => params[:page], :per_page => 5, :order => 'created_at DESC'
    @debtors = User.find(:all, :conditions => 'saldo < 0')
  end

  def show_user_info
    user = User.find(params[:id])
    render :partial => 'user_info', :object => user, :layout => nil
  end

  def prints    
    @waiting_prints = Print.paginate_by_printed false, :page => params[:page], :per_page => 5, :order => 'created_at ASC'
    @printed_prints = Print.paginate_by_printed true, :page => params[:page], :per_page => 5, :order => 'updated_at DESC'
  end

  # do tworzenia listy, emaili, z ktorych korzysta skrypt javy przy auto_complete
  def list
    @emails = User.find(:all, :conditions => ["email LIKE ?", "%#{params[:search]}%"])
  end

  def delete
    @print = Print.find(params[:id])
    @print.destroy
    flash[:notice] = "Druk został usunięty."
    redirect_to :action => "prints"
  end

  def donate
    @user = User.find(params[:id])
    @payments = @user.payments.paginate :page => params[:page], :per_page => 5, :order => 'created_at DESC'
  end

  def add_payment
    u = User.find(params[:id])
    @am = params[:amount]
    u.payments.create :amount => @am
    flash[:notice] = "Dodano " + @am + " zł."
    redirect_to :action => "donate", :id => u.id
  end

  def users
    @emails = User.find(:all, :conditions => ["email LIKE ?", "%#{params[:search]}%"])
    @users = User.paginate :page => params[:page], :per_page => 5, :order => 'email ASC'
  end

  def delete_user
    @user = User.find(params[:id])
    if @user != current_user
      @user.prints.destroy_all
      @user.destroy
      flash[:notice] = "Użytkownik został usunięty."
    else
      flash[:notice] = "Użytkownik nie może usunąć sam siebie."
    end
    redirect_to :action => 'users'
  end

    def delete_payment
    @payment = Payment.find(params[:id])
    @payment.destroy
    flash[:notice] = "Cofnięto transakcję."
    redirect_to :action => 'payments'
  end

  def edit_user_by_email
    @user = User.find_by_email(params[:user][:email])
    if @user != nil
      render :action => 'edit_user'
    else
      flash[:error] = "Wybrano niewłaściwego użytkownika - sprawdź czy zapytanie zawiera adres e-mail!"
      redirect_to :controller => "admin", :action => "users"
    end
  end

  def update
    @user = User.find_by_email(params[:user][:email])
    if @user.update_attributes(params[:user])
      flash[:notice] = 'Dane użytkownika zostały zaktualizowane.'
      redirect_to :action => :users
    else
      render :action => "edit"
    end
  end

  def show_user
      @user = User.find(params[:id])
  end

  def edit_user
      @user = User.find(params[:id])
  end

  def update_user
    @user = User.find(params[:id]) # makes our views "cleaner" and more consistent
    if @user.update_attributes(params[:user])
      flash[:notice] = "Dane zostały zaktualizowane!"
      render :action => "show_user"
    else
      flash[:error] = "Nieprawidłowe dane"
      render :action => 'edit_user', :id => @user.id
    end
  end

  def print
    @print = Print.find(params[:id])
    @user = User.find(:first ,:conditions => {:id => @print.user_id})
  end

  def settings
    @setting = Settings.first
  end

  def update_prices
    setting = Settings.first 
    setting.price_bw = params[:setting][:price_bw]
    setting.price_color = params[:setting][:price_color]
    if setting.save!
      redirect_to :action => "settings"
      flash[:notice] = "Ceny zostały zaktualizowane"
    end
  end

  def messages
    @message = Message.new 
  end
  
  protected

end